<!-- 生成订单 -->
<template>
  <div>
    <OrderFormNavBar></OrderFormNavBar>
    <div class="center">
      <!-- 选择地址 获取到运费 用户名 城市地址id 寄送地址 -->
      <ShippingAddress @addressChanges="addressChanges" @adFreight="adFreight"></ShippingAddress>

      <DetailsList @formChanges="formChanges"></DetailsList>
      <Payment></Payment>
      <FreightWay @Paymentmethod="Paymentmethod"></FreightWay>
      <!-- 
       
       
      :changesUser="changesUser"-->
      <InvoiceInfo  :defaultAddress="defaultAddres"  @invoicetype="invoicetype" @InvoiceInfoIDChange="InvoiceInfoIDChange"></InvoiceInfo>
    </div>
    <TotalPrice
      :Paymentmethods="Paymentmethods"
      :defaultAddress="defaultAddress"
      :changesUser="changesUser"
      :getFreight="getFreight"
      @placeOrder="placeOrder"
      @TotalPriceData="TotalPriceData"
    ></TotalPrice>
  </div>
</template>

<script>
import OrderFormNavBar from "./orderform-component/OrderFormNavBar";
import ShippingAddress from "./myorder-component/ShippingAddress";
import DetailsList from "./myorder-component/DetailsList";
import Payment from "./myorder-component/Payment";
import FreightWay from "./myorder-component/FreightWay";
import InvoiceInfo from "./myorder-component/InvoiceInfo";
import TotalPrice from "./myorder-component/TotalPrice";
export default {
  data() {
    return {
      // fullscreenLoading: false,
      defaultAddress: "", // 收货地址选择
      changesUser: "", // 收货人姓名
      cityId: "", // 收货地址ID
      getFreight: null, //运费
      TotalPaidPrice: 0, //  减掉邮费减掉优惠的总价格

      contractNum: "", // 合同号
      Remarks: "", //备注的内容
      Paymentmethods: 0,

      // steps: 2,

      invoicetype: 2
    };
  },
  methods: {
    // 收货地址激发的父组件的函数
    addressChanges(defaultAddress, changesUser, cityId) {
      this.defaultAddress = defaultAddress;
      this.changesUser = changesUser;
      this.cityId = cityId;
      // console.log(this.defaultAddress,this.changesUser,this.cityId);
    },
    adFreight(Freight) {
      this.getFreight = Freight;
      console.log(this.getFreight, "父组件");
    },
    // 合同号和备注激发的函数
    formChanges(contractNum, Remarks) {
      this.contractNum = contractNum;
      this.Remarks = Remarks;
      // console.log(this.contractNum, this.Remarks);
    },
    //选择到付还是现付
    Paymentmethod(Paymentmethod) {
      this.Paymentmethods = Paymentmethod;
      console.log(this.Paymentmethods, "父组件");
    },
    // 发票地址接收ID
    InvoiceInfoIDChange(id) {
      this.InvoiceAddressID = id;
      console.log(id);
    },
    // 发票地址接收ID
    invoicetype(num = 2) {
      this.invoicetype = num;
      console.log(num);
    },
    // 子組件激发的父组件传值 获取总价
    TotalPriceData(TotalPaidPrice) {
      this.TotalPaidPrice = TotalPaidPrice;
    },
    // 点击提交订单的按钮激发父组件的函数
    async placeOrder() {
      let freight_price = this.getFreight; // 运费
      let user_id = window.localStorage.getItem("user_id"); //用户号
      let receipt_address_id = this.cityId; // 收货地址ID
      let invoice_address_id = this.InvoiceAddressID; // 发票地址ID
      let contract_no = this.contractNum; //合同号
      let remark = this.Remarks; // 备注
      let goods_price = this.TotalPaidPrice; // 订单总价格
      let id_card = this.$store.state.id_card; // 身份证号
      // let invoice_rise = this.$store.state.invoiceRise; // 发票抬头
      let invoice_type = this.invoicetype; // 发票状态 0 或 1 或 2   *****   2为不开发票  0 为普通 1 为专业
      let param = [];

      this.$store.state.SettlementData.forEach((element, index) => {
        // 商品的详细信息 找到有用数据
        param.push({
          id: element.id,
          num: element.num
        });
      });

      console.log(
        "运费",
        freight_price,
        "用户号",
        user_id,
        "收货地址ID",
        receipt_address_id,
        "发票地址ID",
        invoice_address_id,
        "合同号",
        contract_no,
        "备注",
        remark,
        "订单总价格",
        goods_price,
        "身份证号",
        id_card,
        //  '发票抬头', invoice_rise,
        "发票状态 ",
        invoice_type,
        "商品id num",
        param
      );
      if (receipt_address_id) {
        this.fullscreenLoading = true; // 遮罩
        let flag = this.invoicetype;
        console.log(flag);
        if (flag == 1) {
          // 普通发票
          if (invoice_address_id) {
            // 判断收货地址
            let res = await this.$http({
              method: "post",
              url: "index/api/createOrder",
              data: {
                freight_price,
                user_id,
                receipt_address_id,
                invoice_address_id,
                contract_no,
                remark,
                goods_price,
                id_card,
                invoice_rise,
                invoice_type,
                param
              }
            });
            if (res.data.State.Code == 200) {
              console.log(res.data);
              // this.$store.commit("order_no", res.data.order_no); // 把订单号存储在vuex中
              // this.$router.push({
              //   //跳转到支付页面
              //   name: "payment",
              //   params: {}
              // });
              // this.fullscreenLoading = false;
            }
          } else {
            this.fullscreenLoading = false;

            this.$Notify({ type: "info", message: "请填写发票地址..." });
            return;
          }
        } else if (flag == 2) {
          // 专业发票
          if (invoice_address_id) {
            // 判断收货地址
            let res = await this.$http({
              method: "post",
              url: "index/api/createOrder",
              data: {
                freight_price,
                user_id,
                receipt_address_id,
                invoice_address_id,
                contract_no,
                remark,
                goods_price,
                id_card: "",
                invoice_rise,
                invoice_type,
                param
              }
            });
            if (res.data.State.Code == 200) {
              console.log(res.data);
              this.$store.commit("order_no", res.data.order_no); // 把订单号存储在vuex中
              this.$router.push({
                name: "payment",
                params: {}
              });
            }
          } else {
            this.fullscreenLoading = false;
            this.$Notify({ type: "info", message: "请填写发票地址..." });
            return;
          }
        } else {
          // 不开发票
          let res = await this.$http({
            method: "post",
            url: "index/api/createOrder",
            data: {
              freight_price,
              user_id,
              receipt_address_id,
              invoice_address_id: "",
              contract_no,
              remark,
              goods_price,
              id_card: "",
              invoice_rise: "",
              invoice_type,
              param
            }
          });
          if (res.data.State.Code == 200) {
            console.log(res.data);
            this.$store.commit("order_no", res.data.order_no); // 把订单号存储在vuex中
            this.$router.push({
              name: "payment",
              params: {}
            });
            this.fullscreenLoading = false;
          }
        }
      } else {
        this.$Notify({ type: "info", message: "请填写收货地址..." });
      }
    }
  },
  watch: {
    Paymentmethods(oldvalue, newvalue) {
      console.log(oldvalue, newvalue);
    }
    // Remarks(oldvalue, newvalue) {
    //   console.log(oldvalue, newvalue);
    // },
    // cityId(oldvalue, newvalue) {
    //   console.log(oldvalue, newvalue);
    // },
    // freight(oldvalue, newvalue) {
    //   console.log(oldvalue, newvalue);
    // },
  },
  components: {
    OrderFormNavBar,
    ShippingAddress,
    DetailsList,
    Payment,
    InvoiceInfo,
    TotalPrice,
    FreightWay
  }
};
</script>
<style lang='scss' scoped>
.center {
  margin-bottom: 4rem;
}
</style>